package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.IService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.community.ComActivity;
import com.maiji.cloud.entities.community.ComMiddleActivity;
import com.maiji.cloud.mapper.ComActivityMapper;
import com.maiji.cloud.mapper.ComMidActMapper;
import com.maiji.cloud.repository.ComActRepository;
import com.maiji.cloud.request.BaseInputDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.community.ComActMidInputDto;
import com.maiji.cloud.request.community.ComSearchInputDto;
import com.maiji.cloud.response.BaseOutPutDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.community.ComActivityDto;
import com.maiji.cloud.service.AsyncService;
import com.maiji.cloud.service.ComActService;
import com.maiji.cloud.utils.TimeUtil;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class ComActServicelmpl extends ServiceImpl<ComActivityMapper,ComActivity> implements ComActService {


    @Autowired
    AsyncService asyncService;

    @Autowired
    ComActRepository actRepository;

    @Autowired
    ComActivityMapper activityMapper;

    @Autowired
    ComMidActMapper midActMapper;

    public BaseOutPutDto<String> addAct(@RequestBody BaseInputDto<ComActivityDto> inputDto){

        List<ComActivityDto> actSearchInputDtos = new ArrayList<>();
        actSearchInputDtos.add(inputDto.getData());
        BaseInputDto<List<ComActivityDto>> input = new BaseInputDto<>();
        input.setData(actSearchInputDtos);
        return addActs(input);
    }

    /**
     * 添加活动列表
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<String> addActs(@RequestBody BaseInputDto<List<ComActivityDto>> inputDto){

        BaseOutPutDto<String> outPutDto = new BaseOutPutDto<>();
        try {

            List<ComActivity> activities = new ArrayList<>();
            for (ComActivityDto search:inputDto.getData()
            ) {
                ComActivity activity = new ComActivity();
                activity.setActName(search.getActName());
                activities.add(activity);
            }
            asyncService.asycSaveActs(activities);
            activityMapper.insertSearchInputs(activities);

            outPutDto.setStatus(Status.SUCCESS);
            return outPutDto;
        }catch (Exception exc){

            outPutDto.setStatus(Status.ERROR);
            outPutDto.setMessage(exc.getMessage());
            return outPutDto;
        }


    }


    /**
     * 添加热门活动
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<String> addHotAct(@RequestBody BaseInputDto<ComActivityDto>inputDto){

        List<ComActivityDto> actSearchInputDtos = new ArrayList<>();
        actSearchInputDtos.add(inputDto.getData());
        BaseInputDto<List<ComActivityDto>> input = new BaseInputDto<>();
        input.setData(actSearchInputDtos);
        return addHotActs(input);

    }

    /**
     * 添加热门活动列表
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<String> addHotActs(@RequestBody BaseInputDto<List<ComActivityDto>>inputDto){

        BaseOutPutDto<String> outPutDto = new BaseOutPutDto<>();
        try {

            List<ComActivity> activities = new ArrayList();
            for (ComActivityDto search:inputDto.getData()
            ) {
                ComActivity activity = new ComActivity();
                BeanUtils.copyProperties(search,activity);
                activity.setStatus(1);
                activities.add(activity);
            }
            asyncService.asycSaveActs(activities);
            activityMapper.insertSearchInputs(activities);

            outPutDto.setStatus(Status.SUCCESS);
            return outPutDto;
        }catch (Exception exc){

            outPutDto.setStatus(Status.ERROR);
            outPutDto.setMessage(exc.getMessage());
            return outPutDto;
        }
    }

    /**
     * 绑定动态到多个活动
     * @param
     * @return
     */
    public BaseOutPutDto<String> addMsgActs(@RequestBody BaseInputDto<List<ComActMidInputDto>> inputDto){
        BaseOutPutDto<String>outPutDto = new BaseOutPutDto<>();
        try{
            List<ComMiddleActivity> middleActivities = new ArrayList<>();
            for (ComActMidInputDto actMidInputDto:inputDto.getData()
                 ) {
                ComMiddleActivity middleActivity = new ComMiddleActivity();
                BeanUtils.copyProperties(actMidInputDto,middleActivity);
                middleActivity.setPublishTime(TimeUtil.getCurDateStamp());
                middleActivity.setIsTop(0);
                middleActivity.setTopSort(0);
                middleActivities.add(middleActivity);
            }
            
            midActMapper.inserts(middleActivities);
            outPutDto.setStatus(Status.SUCCESS);
            outPutDto.setMessage("绑定成功");
            return outPutDto;
        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            outPutDto.setMessage(exc.getMessage());
            return outPutDto;
        }

    }

    /**
     * 删除活动
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<String> deleteAct(@RequestBody BaseInputDto<ComActivityDto>inputDto){

        BaseOutPutDto<String>outPutDto = new BaseOutPutDto<>();
        try{

            ComActivity activity = new ComActivity();
            activity.setActName(inputDto.getData().getActName());
            if(activityMapper.deleteById(inputDto.getData().getActName())>0){
                outPutDto.setMessage("删除成功");
                outPutDto.setStatus(Status.SUCCESS);
            }
            return outPutDto;
        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            outPutDto.setMessage(exc.getMessage());
            return outPutDto;
        }

    }

    /**
     * 修改活动
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<String> updateAct(@RequestBody BaseInputDto<ComActivityDto>inputDto){

        BaseOutPutDto<String>outPutDto = new BaseOutPutDto<>();
        try{

            ComActivity activity = new ComActivity();
            BeanUtils.copyProperties(inputDto.getData(),activity);
            if(activityMapper.updateById(activity)>0){
                outPutDto.setMessage("修改成功");
                outPutDto.setStatus(Status.SUCCESS);
            }
            return outPutDto;
        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            outPutDto.setMessage(exc.getMessage());
            return outPutDto;
        }

    }



    /**
     * 搜索活动
     * @param inputDto
     * @return
     */
    public BaseOutPutDto<List<ComActivityDto>> searchActs(@RequestBody BaseInputDto<ComSearchInputDto> inputDto){
        BaseOutPutDto<List<ComActivityDto>> outPutDto = new BaseOutPutDto<>();

        try{

            ReqMetaData metaData = inputDto.getMetaData();
            PageRequest pageable = new PageRequest((metaData.getPage() - 1) * metaData.getSize(),metaData.getSize());
            QueryStringQueryBuilder builder = new QueryStringQueryBuilder(inputDto.getData().getText());

            SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withPageable(pageable)
                    .withQuery(builder)
                    .build();

            Page<ComActivity> activities = actRepository.search(searchQuery);
            outPutDto.setData(reloadActivityList(activities.getContent()));
            outPutDto.setStatus(Status.SUCCESS);
            return outPutDto;

        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            return outPutDto;
        }

    }

    /**
     * 热门活动列表
     * @return
     */
    public BaseOutPutDto<List<ComActivityDto>> hotActs(){
        BaseOutPutDto<List<ComActivityDto>> outPutDto = new BaseOutPutDto<>();

        try{

            ComActivity comActivity = new ComActivity();
            EntityWrapper<ComActivity> entityWrapper = new EntityWrapper<>();
            entityWrapper.setEntity(comActivity);
            entityWrapper.orderBy("sort",true);
            entityWrapper.where("status > 0");

            List<ComActivity> activities = activityMapper.selectList(entityWrapper);

            outPutDto.setStatus(Status.SUCCESS);
            outPutDto.setData(reloadActivityList(activities));
            return outPutDto;

        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            return outPutDto;
        }

    }

    /**
     * 精选活动列表
     * @return
     */
    public BaseOutPutDto<List<ComActivityDto>> essActs(){

        BaseOutPutDto<List<ComActivityDto>> outPutDto = new BaseOutPutDto<>();

        try{

            ComActivity comActivity = new ComActivity();
            comActivity.setStatus(2);

            EntityWrapper<ComActivity> entityWrapper = new EntityWrapper<>();
            entityWrapper.setEntity(comActivity);
            entityWrapper.orderBy("sort",true);
            List<ComActivity> activities = activityMapper.selectList(entityWrapper);

            outPutDto.setStatus(Status.SUCCESS);
            outPutDto.setData(reloadActivityList(activities));
            return outPutDto;

        }catch (Exception exc){
            outPutDto.setStatus(Status.ERROR);
            return outPutDto;
        }
    }


    private   List<ComActivityDto> reloadActivityList(List<ComActivity>activities){

        List<ComActivityDto> activityOutputDtos = new ArrayList();
        for (ComActivity activity: activities
        ) {
            ComActivityDto activityOutputDto = new ComActivityDto();
            BeanUtils.copyProperties(activity,activityOutputDto);
            activityOutputDtos.add(activityOutputDto);
        }

        return activityOutputDtos;
    }

}
